<%@ Page Language="C#" %>
<%@ Import namespace="IFXplus.Northwind"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected static string id = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        id = Request.QueryString["id"] + "";
        if (!IsPostBack)
        {
            Customer c = CustomerManager.GetCustomer(id);
            if (c == null)
            {
                lblMessage.Text = "Impossibile trovare customer con id = \"" + id + "\"";
                lblMessage.Visible = true;
                phDetails.Visible = false;
            }
            else
            {
                btnView.HRef = "customerview.aspx?id=" + id;
                txtCustomerID.Text = c.CustomerId;
                txtCompanyName.Text = c.CompanyName;
                txtContactName.Text = c.ContactName;
                txtContactTitle.Text = c.ContactTitle;
                txtAddress.Text = c.Address;
                txtCity.Text = c.City;
                txtRegion.Text = c.Region;
                txtPostalCode.Text = c.PostalCode;
                txtCountry.Text = c.Country;
                txtPhone.Text = c.Phone;
                txtFax.Text = c.Fax;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CustomerManager.UpdateCustomer(
                id,
                txtCompanyName.Text,
                txtContactName.Text,
                txtContactTitle.Text,
                txtAddress.Text,
                txtCity.Text,
                txtRegion.Text,
                txtPostalCode.Text,
                txtCountry.Text,
                txtPhone.Text,
                txtFax.Text);
            Response.Redirect("customerview.aspx?id=" + id);
        }
        catch (Exception ex)
        {
            lblMessage.Text = "ERRORE: " + ex.Message;
            lblMessage.Visible = true;
            phDetails.Visible = false;
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Customer Edit</title>
</head>
<body>
	<form id="frmMain" runat="server">
	<div>
		<asp:Label ID="lblMessage" runat="server" Text="" Visible="false"></asp:Label>
		<asp:PlaceHolder ID="phDetails" runat="server">
			CustomerID = <asp:TextBox ID="txtCustomerID" runat="server" Text="" ReadOnly="true"></asp:TextBox><br />
			CompanyName = <asp:TextBox ID="txtCompanyName" runat="server" Text=""></asp:TextBox><br />
			ContactName = <asp:TextBox ID="txtContactName" runat="server" Text=""></asp:TextBox><br />
			ContactTitle = <asp:TextBox ID="txtContactTitle" runat="server" Text=""></asp:TextBox><br />
			Address = <asp:TextBox ID="txtAddress" runat="server" Text=""></asp:TextBox><br />
			City = <asp:TextBox ID="txtCity" runat="server" Text=""></asp:TextBox><br />
			Region = <asp:TextBox ID="txtRegion" runat="server" Text=""></asp:TextBox><br />
			PostalCode = <asp:TextBox ID="txtPostalCode" runat="server" Text=""></asp:TextBox><br />
			Country = <asp:TextBox ID="txtCountry" runat="server" Text=""></asp:TextBox><br />
			Phone = <asp:TextBox ID="txtPhone" runat="server" Text=""></asp:TextBox><br />
			Fax = <asp:TextBox ID="txtFax" runat="server" Text=""></asp:TextBox><br />
			<br />
		</asp:PlaceHolder>
		<hr />
		<asp:LinkButton ID="btnSave" runat="server" Text="Salva" OnClick="btnSave_Click" /> | <a id="btnView" runat="server">Annulla</a> | <a href="default.aspx">Torna alla lista</a>
    </div>
    </form>
</body>
</html>